//-----------------------------------------------------------------------------------
#include "CMyComboBox.h"
#include "flecheBas.png.cpp"
//-----------------------------------------------------------------------------------
CMyComboBox::CMyComboBox(QWidget *parent) : QWidget(parent), CChampText()
{
	int btSize;
	
	setFocusPolicy((Qt::FocusPolicy)(Qt::TabFocus | Qt::ClickFocus));
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
	
	setDataType(CCommun::edtString);
	
	lineEdit=new QLineEdit(this);
	QObject::connect(lineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(OnTextEdited(const QString&)));
	QObject::connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(OnEditingFinished()));
	lineEdit->show();
	btSize=lineEdit->sizeHint().height();
	
	pushButton=new QPushButton(getImageFromMemory(flecheBas_png, sizeof(flecheBas_png)), "", this);
	pushButton->setFocusPolicy(Qt::NoFocus);
	pushButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	pushButton->setMinimumWidth(btSize);
	pushButton->setMaximumWidth(btSize);
	pushButton->setMinimumHeight(btSize);
	pushButton->setMaximumHeight(btSize);
	QObject::connect(pushButton, SIGNAL(clicked(void)), this, SLOT(OnClicked(void)));
	pushButton->show();
	
	QHBoxLayout *layout=new QHBoxLayout();
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);
	layout->addWidget(lineEdit);
	layout->addWidget(pushButton);
	setLayout(layout);
	
	setFocusProxy(lineEdit);
}
//-----------------------------------------------------------------------------------
QString CMyComboBox::getValue(void)
{
	return lineEdit->text().replace("\"", "\\\"");
}
//-----------------------------------------------------------------------------------
void CMyComboBox::setValue(QVariant value)
{
	lineEdit->setText(toString(value));
}
//-----------------------------------------------------------------------------------
void CMyComboBox::setStateEnable(bool enable)
{
	if(_standaloneEnable)
	{
		setEnabled(enable);
	}
}
//-----------------------------------------------------------------------------------
void CMyComboBox::setAttribut(EAttribut value)
{
	QString oldToolTip=toolTip();
	_attribut=value;
	
	lineEdit->setStyleSheet(makeStyleSheet(value, oldToolTip));
	lineEdit->setToolTip(oldToolTip);
}
//-----------------------------------------------------------------------------------
void CMyComboBox::setTextCondition(QString value)
{
	_textCondition=value;
	if(getAttribut() & atObligatoireConditionel)
	{
		lineEdit->setToolTip(value);
	}
}
//------------------------------------------------------------------------------
bool CMyComboBox::isEmpty(void)
{
	return toBool(lineEdit->text());
}
//------------------------------------------------------------------------------
void CMyComboBox::OnClicked(void)
{
	QString oldValue=getValue();
	
	emit clicked(this);
	
	if(oldValue != getValue())
	{
		emit textEdited(lineEdit->text());
	}
}
//-----------------------------------------------------------------------------------
void CMyComboBox::OnTextEdited(QString text)
{
	emit textEdited(text);
}
//-----------------------------------------------------------------------------------
void  CMyComboBox::OnEditingFinished(void)
{
	emit editingFinished();
}
//-----------------------------------------------------------------------------------
/*void CMyComboBox::resizeEvent(QResizeEvent *event)
{
	int h=size().height();
	int w=size().width();
	
	lineEdit->setFixedSize(w-h, h);
	pushButton->setFixedSize(h, h);
	pushButton->move(w-h, 0);
	
	QWidget::resizeEvent(event);
}*/
//-----------------------------------------------------------------------------------
QPixmap CMyComboBox::getImageFromMemory(const unsigned char *data, int len)
{
	QPixmap img;
	 
	img.loadFromData(data, len, 0);
	
	return img;
}
//-----------------------------------------------------------------------------------
void CMyComboBox::keyPressEvent(QKeyEvent * event)
{
	if(event->key() == Qt::Key_F1)
	{
		emit invokeHelp(this);
	}
	QWidget::keyPressEvent(event);
}
//------------------------------------------------------------------------------

